package edu.kit.csl.cratylus.io;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import edu.kit.csl.cratylus.datatypes.Corpus;
import edu.kit.csl.cratylus.datatypes.SentencePair;
import edu.kit.csl.cratylus.extraction.WritingSystem;

/**
 * This corpus dumper generates two separate files for target and source
 * language. They contain the plain word sequences separated by line breaks.
 * This format is (together with the GIZA++ alignment file) suitable for the
 * SMT toolkit Moses. Note that in this representation, the alignments them 
 * self are not reconstructible anymore.
 * 
 * @see GizaCorpusDumper
 */
public class PlainCorpusDumper implements CorpusDumper {

	/**
	 * Writes the both files <code>fileName</code>.src and <code>fileName
	 * </code>.trgt containing line by line the plain word sequences in both
	 * languages. Note that in this representation, the alignments them self
	 * are not reconstructible anymore.
	 * 
	 * @param corpus the corpus to dump
	 * @param ws the writing system to use
	 * @param fileName the path to the file to write
	 * @throws NullPointerException if one of the arguments is null
	 * @throws IOException if writing to the file system was not successful
	 */
	@Override
	public void dumpToFileSystem(Corpus corpus, WritingSystem ws,
			String fileName) throws IOException {
		FileWriter srcFs = new FileWriter(fileName + ".src");
		FileWriter trgtFs = new FileWriter(fileName + ".trgt");
		BufferedWriter srcOut = new BufferedWriter(srcFs);
		BufferedWriter trgtOut = new BufferedWriter(trgtFs);
		for (SentencePair pair : corpus) {
			srcOut.write(pair.getSourceSentence().toString() + "\n");
			trgtOut.write(pair.getTargetSentence().toString(ws) + "\n");
		}
		srcOut.close();
		trgtOut.close();
	}
}
